//
//  UIColor+AD.swift
//  ADKit
//
//  Created by Adoma on 2020/10/22.
//

import UIKit

public extension UIColor {
    
    /// by ADKit
    static var random: UIColor {
        return UIColor(hex: .random(in: 0...0xffffff), alpha: .random(in: 0.5...1))
    }

    /// RGB/RGBA/RRGGBB/RRGGBBAA
    /// by ADKit
    convenience init(hex: Int, alpha: CGFloat? = nil) {
        self.init(hex: String(format: "%X", hex), alpha: alpha)
    }
    
    /// RGB/RGBA/RRGGBB/RRGGBBAA
    /// by ADKit
    convenience init(hex: String, alpha: CGFloat? = nil) {
  
        let str = hex.trimmingCharacters(in: .whitespacesAndNewlines)
            .uppercased()
            .replacingOccurrences(of: "^(#|0X)", with: "", options: .regularExpression)

        var r: CGFloat = 1
        var g: CGFloat = 1
        var b: CGFloat = 1
        var a: CGFloat = 1
        
        let length = str.count

        if length < 6 {
            if length > 0 {
                r = CGFloat(str[0].toHexValue) / 255
            }
            if length > 1 {
                g = CGFloat(str[1].toHexValue) / 255
            }
            if length > 2 {
                b = CGFloat(str[2].toHexValue) / 255
            }
            if length > 3 {
                a = CGFloat(str[3...].toHexValue) / 255
            }
        } else {
            r = CGFloat(str[0..<2].toHexValue) / 255
            g = CGFloat(str[2..<4].toHexValue) / 255
            b = CGFloat(str[4..<6].toHexValue) / 255
            if length > 6 {
                a = CGFloat(str[6...].toHexValue) / 255
            }
        }
        
        self.init(red: r, green: g, blue: b, alpha: alpha ?? a)
    }
    
    /// ARGB/AARRGGBB
    /// by ADKit
    convenience init?(ahex: String) {
        
        let str = ahex.trimmingCharacters(in: .whitespacesAndNewlines)
            .uppercased()
            .replacingOccurrences(of: "^(#|0X)", with: "", options: .regularExpression)
        
        let length = str.count
        
        if length == 0 {
            return nil
        }
        
        let hex: String
        let alpha: CGFloat
        
        if length == 1 || length == 3 || length == 6 {
            alpha = 1
            hex = str
        }
        else if length <= 4 {
            alpha = CGFloat(str[0].toHexValue) / 255
            hex = str[1...]
        }
        else {
            alpha = CGFloat(str[0..<2].toHexValue) / 255
            hex = str[2...]
        }
        
        self.init(hex: hex, alpha: alpha)
    }
    
    func alpha(_ alpha: CGFloat) -> UIColor {
        withAlphaComponent(alpha)
    }
}

public extension String {
    
    var toColor: UIColor { UIColor(hex: self) }
    
    var toAhexColor: UIColor? { UIColor(ahex: self) }
    
}

public extension UIColor {
    
    func dark(_ color: UIColor) -> UIColor {
        if #available(iOS 13, *) {
            return UIColor { trainCollection in
                if trainCollection.userInterfaceStyle == .dark {
                    return color
                }
                return self
            }
        }
        return self
    }
}
